﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Spider
{
    /// <summary>
    /// 处理HTML文档
    /// </summary>
    public class ParseHtmlNew
    {
        /// <summary>
        /// 根据正则表达式得到页面里的东西
        /// </summary>
        /// <param name="page"></param>
        /// <param name="regex"></param>
        /// <returns></returns>
        public static IList<string> GetHyperLink(string page, string regex)
        {
            IList<string> list = new List<string>();
            System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(regex);
            System.Text.RegularExpressions.MatchCollection matches = re.Matches(page);

            System.Collections.IEnumerator enu = matches.GetEnumerator();
            while (enu.MoveNext() && enu.Current != null)
            {
                System.Text.RegularExpressions.Match match = (System.Text.RegularExpressions.Match)(enu.Current);
                Log.LogSpecial(LogDetail.LogType.Others, match.Value);
                string str = match.Value.Replace("href=", "").Replace("\"", "").Replace("'", "");
                list.Add(str);
            }
            return list;
        }
        /// <summary>
        /// 根据正则表达式得到页面里的链接，href和src
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public static IList<string> GetHyperLink(string page)
        {
            IList<string> list = new List<string>();

            #region 查找href链接
            string regex = "href=[\\\"\\\'](http:\\/\\/|\\.\\/|\\/)?\\w+(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?[\\\"\\\']";
            System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(regex);
            System.Text.RegularExpressions.MatchCollection matches = re.Matches(page);

            System.Collections.IEnumerator enu = matches.GetEnumerator();
            while (enu.MoveNext() && enu.Current != null)
            {
                System.Text.RegularExpressions.Match match = (System.Text.RegularExpressions.Match)(enu.Current);
                Log.LogSpecial(LogDetail.LogType.Others, match.Value);
                string str = match.Value.Replace("href=", "").Replace("\"", "").Replace("'", "");
                list.Add(str);
            }
            #endregion

            #region 查找src链接
            regex = "src=[\\\"\\\'](http:\\/\\/|\\.\\/|\\/)?\\w+(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?[\\\"\\\']";
            re = new System.Text.RegularExpressions.Regex(regex);
            matches = re.Matches(page);

            enu = matches.GetEnumerator();
            while (enu.MoveNext() && enu.Current != null)
            {
                System.Text.RegularExpressions.Match match = (System.Text.RegularExpressions.Match)(enu.Current);
                Log.LogSpecial(LogDetail.LogType.Others, match.Value);
                string str = match.Value.Replace("src=", "").Replace("\"", "").Replace("'", "");
                list.Add(str);
            }
            #endregion

            return list;
        }
        #region 逐个html标记查找
        public static IList<string> GetHyperLinkByParse(string page)
        {

            ParseHTML parse = new ParseHTML();
            parse.Source = page;//ParseHTML类，提供对HTML的解析，然后将它的source属性设置为要解析的HTML文档
            IList<string> list = new List<string>();
            try
            {
                while (!parse.Eof())
                {
                    char ch = parse.Parse();
                    //Parse方法将返回HTML文档包含的字符，它返回的内容只包含那些非HTML标记的字符，如果遇到了HTML标记，
                    //Parse方法将返回0值，表示现在遇到了一个HTML标记。遇到一个标记之后，我们可以用GetTag()方法来处理它
                    if (ch == 0)
                    {
                        Attribute a = parse.GetTag()["HREF"];  //获取Attribute对象之后，通过Attribute.Value可以得到该属性的值
                        if (a != null)
                        {
                            if (a.Value.Length > 100)
                            {
                                a.Value = a.Value.Substring(0, 100);
                            }
                            if (!a.Value.Contains("java"))
                            {
                                if (!list.Contains(a.Value))
                                {
                                    list.Add(a.Value);
                                    Log.LogSpecial(LogDetail.LogType.Others, a.Value);
                                }
                            }
                        }
                        a = parse.GetTag()["SRC"];
                        if (a != null)
                        {
                            if (a.Value.Length > 100)
                            {
                                a.Value = a.Value.Substring(0, 100);
                            }
                            if (!a.Value.Contains("java"))
                            {
                                if (!list.Contains(a.Value))
                                {
                                    list.Add(a.Value);
                                    Log.LogSpecial(LogDetail.LogType.Others, a.Value);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception err)
            {
                Log.LogSpecial(LogDetail.LogType.GoloalRecord, err.Message);
            }
            return list;
        }
        #endregion
    }
}
